extends Control

#注意 在加载场景树中节点时，onready非常重要
@onready var my_sprite = get_parent()
@onready var health_bar: ProgressBar = %HealthBar
@onready var anger_bar_1: ProgressBar = %AngerBar1
@onready var anger_bar_2: ProgressBar = %AngerBar2
@onready var anger_bar_3: ProgressBar = %AngerBar3
@onready var anger_bar_4: ProgressBar = %AngerBar4
@onready var health_label: Label = %HealthLabel
@onready var health_reduce_label: Label = %HealthReduceLabel



func _ready() -> void:
	Events.signal_health_reduce.connect(_display_health_reduced)
	Events.signal_health_regeneration.connect(_display_health_regeneration)
	health_bar.max_value = my_sprite.max_health
	anger_bar_1.value = 0
	anger_bar_2.value = 0
	anger_bar_3.value = 0
	anger_bar_4.value = 0
	
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
	health_bar.value = my_sprite.health
	health_label.text = str(my_sprite.health) + " / " + str(my_sprite.max_health)
	match my_sprite.anger:
		0:
			anger_bar_1.value = 0
			anger_bar_2.value = 0
			anger_bar_3.value = 0
			anger_bar_4.value = 0
		1:
			anger_bar_1.value = 100
			anger_bar_2.value = 0
			anger_bar_3.value = 0
			anger_bar_4.value = 0
		2:
			anger_bar_1.value = 100
			anger_bar_2.value = 100
			anger_bar_3.value = 0
			anger_bar_4.value = 0
		3:
			anger_bar_1.value = 100
			anger_bar_2.value = 100
			anger_bar_3.value = 100
			anger_bar_4.value = 0
		4:
			anger_bar_1.value = 100
			anger_bar_2.value = 100
			anger_bar_3.value = 100
			anger_bar_4.value = 100

func _display_health_reduced(health_reduced_sprite: StaticBody2D, damage: int) -> void:
	if health_reduced_sprite == my_sprite:
		health_reduce_label.add_theme_color_override("font_color", "f80300")
		health_reduce_label.visible = true
		health_reduce_label.text = "-" + str(damage)
		await get_tree().create_timer(0.8).timeout
		health_reduce_label.visible = false
		
func _display_health_regeneration(health_regeneration_sprite: StaticBody2D, health_value: int) -> void:
	if health_regeneration_sprite == my_sprite:
		health_reduce_label.add_theme_color_override("font_color", "00f731")
		health_reduce_label.visible = true
		health_reduce_label.text = "+" + str(health_value)
		await get_tree().create_timer(0.8).timeout
		health_reduce_label.visible = false
